/*******************************************************************************
 * Copyright (c) 2009 David Harrison.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v3.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/gpl-3.0.html 
 *  
 * Contributors:
 *     David Harrison - initial API and implementation
 ******************************************************************************/
package com.sfs.postcodes.stripes;

/**
 *
 * @author David Harrison
 * 3 May 2008
 * 
 */

import com.sfs.postcodes.beans.GeocodeBean;
import com.sfs.postcodes.dao.GeocoderDAO;
import com.sfs.postcodes.dao.PostcodeDAO;

import java.util.StringTokenizer;

import net.sourceforge.stripes.integration.spring.SpringBean;


public abstract class LookupBaseActionBean extends BaseActionBean {
                
    private String address = "";
    
    private String state = "";
    
    private String country = "";
    
    private String postcode = "";
    
    private GeocodeBean geocode; 
    
    private PostcodeDAO postcodeDAO;
    
    private GeocoderDAO geocoderDAO;
    
    
    /**
     * Setter to allow the PostcodeDAO to be injected.
     * @param postcodeDAO
     */
    @SpringBean("postcodeDAO")
    public void injectPostcodeDAO(PostcodeDAO postcodeDAO) {
        this.postcodeDAO  = postcodeDAO;
    }
    
    /** 
     * Setter to allow the GeocoderDAO to be injected.     
     * @param geocoderDAO
     */
    @SpringBean("geocoderDAO")
    public void injectGeocoderDAO(GeocoderDAO geocoderDAO) {
        this.geocoderDAO  = geocoderDAO;
    }   
    
    protected PostcodeDAO getPostcodeDAO() {
        return this.postcodeDAO;
    }
    
    protected GeocoderDAO getGeocoderDAO() {
        return this.geocoderDAO;
    }
    
    public void setAddress(final String address) {
        this.address = address;
    }
    public String getAddress() {
        return this.address;
    }
    
    public String getFormattedAddress() {
        return convertAddress(", ");
    }
    
    public String getHtmlAddress() {
        return convertAddress("<br/>");
    }
    
    public void setState(final String state) {
        this.state = state;
    }
    public String getState() {
        return this.state;
    }
    
    public void setCountry(final String country) {
        this.country = country;
    }
    public String getCountry() {
        return this.country;
    }
    
    protected void setPostcode(final String postcode) {
        this.postcode = postcode;
    }
    public String getPostcode() {
        return this.postcode;
    }
    public boolean getPostcodeSet() {
        boolean exists = false;
        if(postcode!=null && postcode.compareTo("")!=0) {
            exists = true;
        }
        return exists;
    }
    
    protected void setGeocode(final GeocodeBean geocode) {
        this.geocode = geocode;
    }
    public GeocodeBean getGeocode() {
        return this.geocode;
    }
    public boolean getGeocodeSet() {
        boolean exists = false;
        if(geocode!=null) {
            exists = true;
        }
        return exists;
    }
    
    private String convertAddress(final String lineBreak)
    {
        StringBuffer convertedAddress = new StringBuffer();
        if(this.address!=null) {
            StringTokenizer tk = new StringTokenizer(this.address, "\n");
            while(tk.hasMoreTokens()) {
                String token = tk.nextToken();
                if(convertedAddress.length()>0) {
                    convertedAddress.append(lineBreak);
                }
                convertedAddress.append(token.trim());
            }
        }
        return convertedAddress.toString();
    }
}
